package com.aurora.app.component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.socket.WebSocketSession;

import com.aurora.base.util.JedisUtil;

import redis.clients.jedis.Jedis;

/**
 * websocketsession管理类
 * 
 * @author yongjiang.lin
 *
 */
@Component
public class SocketSessionManagerV2 implements SocketSessionManager {
	/**
	 * sessionid用户之间的联系
	 */
	private static final String session_user_map = "sessionUserConnection";
	
	/**
	 * 用途注册表
	 */
	private static final String register_useage_map = "registerUseageTable";
	
	/**
	 * userid - session 维护用户和session实例的关系
	 */
	public static final ConcurrentMap<String, WebSocketSession> userSocketSessionMap;
	
	/**
	 * 利用redis作高速缓存
	 */
	public static final JedisUtil jedisUtil;
	static {
		userSocketSessionMap = new ConcurrentHashMap<String, WebSocketSession>();
		jedisUtil = JedisUtil.getInstance();
	}

	@Override
	public void addSession(String userId, WebSocketSession session) {
        Jedis jedis = jedisUtil.getJedis();
        jedis.del(session.getId());
        /**
         * 第一个元素必须为userId
         */
        jedis.lpush(session.getId(),userId);
        List<String> list = jedis.lrange(session.getId(), 0, -1);
        jedisUtil.closeJedis(jedis);
		userSocketSessionMap.put(userId, session);
		System.out.println("addSession");
		System.out.println(list);
	}

	@Override
	public void remove(String userId) {
		WebSocketSession session =  userSocketSessionMap.get(userId);
		if(session!=null){
			Jedis jedis = jedisUtil.getJedis();
			jedis.del(session.getId());
			userSocketSessionMap.remove(userId);
			jedisUtil.closeJedis(jedis);
		}
		
	}

	@Override
	public void remove(WebSocketSession session) {
		Jedis jedis = jedisUtil.getJedis();
		List<String> list = jedis.lrange(session.getId(),0,1);
		if(!CollectionUtils.isEmpty(list)){
			String userId = list.get(0);
			userSocketSessionMap.remove(userId);
			jedis.del(session.getId());
		}
		jedisUtil.closeJedis(jedis);
	}

	@Override
	public void registerUseage(String sessionId, String useage) {
		Jedis jedis = jedisUtil.getJedis();
		List<String> list = jedis.lrange(sessionId, 0, -1);
		if(!list.contains(useage)){
			jedis.lpush(sessionId,useage);
		}
	    jedisUtil.closeJedis(jedis);
	}

	@Override
	public void removeUseage(String sessionId, String useage) {
		Jedis jedis = jedisUtil.getJedis();
		List<String> list = jedis.lrange(sessionId, 0, -1);
		if(list.contains(useage)){
			list.remove(useage);
		}
	    jedisUtil.closeJedis(jedis);
	}

	@Override
	public void removeAllUseage(String sessionId) {
		Jedis jedis = jedisUtil.getJedis();
		List<String> list = jedis.lrange(sessionId, 0, -1);
		jedis.del(sessionId);
		jedis.lpush(sessionId, list.get(0));
	    jedisUtil.closeJedis(jedis);
	}

	@Override
	public Boolean isCanSend(String sessionId, String userId, String useage) {
		if(!userSocketSessionMap.containsKey(userId)){
			return false;
		}
		Jedis jedis = jedisUtil.getJedis();
		List<String> list = jedis.lrange(sessionId, 0, -1);
		if(!list.contains(useage)){
			return false;
		}
		return true;
	}

	@Override
	public WebSocketSession getSession(String userId) {
		return  userSocketSessionMap.get(userId);
	}
}
